package jnpf.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_XiangMuBianGengEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.mapper.ZXGL_XiangMuBianGengMapper;
import jnpf.model.zxgl_xiangmubiangeng.ZXGL_XiangMuBianGengPagination;
import jnpf.service.ZXGL_XiangMuBianGengService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.service.ZXGL_XiangMuLiXiang_MingXiService;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * ZXGL_XiangMuBianGeng
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-22
 */
@Slf4j
@Service
public class ZXGL_XiangMuBianGengServiceImpl extends ServiceImpl<ZXGL_XiangMuBianGengMapper, ZXGL_XiangMuBianGengEntity> implements ZXGL_XiangMuBianGengService {

    private static final Logger logger = LoggerFactory.getLogger(ZXGL_XiangMuBianGengServiceImpl.class);

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zxglXiangMuLiXiangMingXiService;

    @Autowired
    private ObjectMapper objectMapper;

    @Resource
    PermissionsConfig permissionsConfig;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;

    @Override
    public List<ZXGL_XiangMuBianGengEntity> getList(ZXGL_XiangMuBianGengPagination zXGL_XiangMuBianGengPagination) {

        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuBianGengEntity> zXGL_XiangMuBianGengQueryWrapper = new QueryWrapper<>();
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator()) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                //项目变更：创建人看本人数据；主责部门负责人看本部门数据；主责部门主管领导看所管理部门数据；
                zXGL_XiangMuBianGengQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_XiangMuBianGengEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_XiangMuBianGengEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门数据
                        .or()
                        .eq(ZXGL_XiangMuBianGengEntity::getDepartmentHeadleaderZzbm, userId)//主责部门主管领导看所管理部门数据
                );
            }
        }
        int zXGL_XiangMuBianGengNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuBianGengPagination.getBglx())) {
            zXGL_XiangMuBianGengNum++;
            zXGL_XiangMuBianGengQueryWrapper.lambda().like(ZXGL_XiangMuBianGengEntity::getBglx, zXGL_XiangMuBianGengPagination.getBglx());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuBianGengPagination.getXmid())) {
            zXGL_XiangMuBianGengNum++;
            zXGL_XiangMuBianGengQueryWrapper.lambda().eq(ZXGL_XiangMuBianGengEntity::getXmid, zXGL_XiangMuBianGengPagination.getXmid());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuBianGengQueryWrapper.lambda().in(ZXGL_XiangMuBianGengEntity::getId, AllIdList);
        }
        //排序
//        if (StringUtil.isEmpty(zXGL_XiangMuBianGengPagination.getSidx())) {
//            zXGL_XiangMuBianGengQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuBianGengEntity::getBgrq);
//        } else {
//            try {
//                String sidx = zXGL_XiangMuBianGengPagination.getSidx();
//                ZXGL_XiangMuBianGengEntity zXGL_XiangMuBianGengEntity = new ZXGL_XiangMuBianGengEntity();
//                Field declaredField = zXGL_XiangMuBianGengEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_XiangMuBianGengQueryWrapper = "asc".equals(zXGL_XiangMuBianGengPagination.getSort().toLowerCase()) ? zXGL_XiangMuBianGengQueryWrapper.orderByAsc(value) : zXGL_XiangMuBianGengQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_XiangMuBianGengQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuBianGengEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_XiangMuBianGengEntity> page = new Page<>(zXGL_XiangMuBianGengPagination.getCurrentPage(), zXGL_XiangMuBianGengPagination.getPageSize());
            IPage<ZXGL_XiangMuBianGengEntity> userIPage = this.page(page, zXGL_XiangMuBianGengQueryWrapper);
            return zXGL_XiangMuBianGengPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_XiangMuBianGengEntity> list = new ArrayList();
            return zXGL_XiangMuBianGengPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_XiangMuBianGengEntity> getTypeList(ZXGL_XiangMuBianGengPagination zXGL_XiangMuBianGengPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuBianGengEntity> zXGL_XiangMuBianGengQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuBianGengNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuBianGengPagination.getBglx())) {
            zXGL_XiangMuBianGengNum++;
            zXGL_XiangMuBianGengQueryWrapper.lambda().like(ZXGL_XiangMuBianGengEntity::getBglx, zXGL_XiangMuBianGengPagination.getBglx());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuBianGengQueryWrapper.lambda().in(ZXGL_XiangMuBianGengEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_XiangMuBianGengPagination.getSidx())) {
            zXGL_XiangMuBianGengQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuBianGengEntity::getBgrq);
        } else {
            try {
                String sidx = zXGL_XiangMuBianGengPagination.getSidx();
                ZXGL_XiangMuBianGengEntity zXGL_XiangMuBianGengEntity = new ZXGL_XiangMuBianGengEntity();
                Field declaredField = zXGL_XiangMuBianGengEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_XiangMuBianGengQueryWrapper = "asc".equals(zXGL_XiangMuBianGengPagination.getSort().toLowerCase()) ? zXGL_XiangMuBianGengQueryWrapper.orderByAsc(value) : zXGL_XiangMuBianGengQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_XiangMuBianGengEntity> page = new Page<>(zXGL_XiangMuBianGengPagination.getCurrentPage(), zXGL_XiangMuBianGengPagination.getPageSize());
                IPage<ZXGL_XiangMuBianGengEntity> userIPage = this.page(page, zXGL_XiangMuBianGengQueryWrapper);
                return zXGL_XiangMuBianGengPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_XiangMuBianGengEntity> list = new ArrayList();
                return zXGL_XiangMuBianGengPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_XiangMuBianGengQueryWrapper);
        }
    }

    @Override
    public ZXGL_XiangMuBianGengEntity getInfo(String id) {
        QueryWrapper<ZXGL_XiangMuBianGengEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuBianGengEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_XiangMuBianGengEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_XiangMuBianGengEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public boolean updateFlow(String id) {
        logger.info("进入变更回调Id===================》" + id);
        ZXGL_XiangMuBianGengEntity zxglXiangMuBianGengEntity = baseMapper.selectById(id);
        logger.info("进入变更回调zxglXiangMuBianGengEntity===================》" + zxglXiangMuBianGengEntity);
        ZXGL_XiangMuLiXiang_MingXiEntity zxglXiangMuLiXiangMingXiEntity = zxglXiangMuLiXiangMingXiService.getById(zxglXiangMuBianGengEntity.getXmid());

        if (null != zxglXiangMuBianGengEntity) {
            String xmzt = "";
            //变更类型选的哪个更新那个
            if (null != zxglXiangMuBianGengEntity.getBglx()) {
                if (null != zxglXiangMuBianGengEntity.getBglx()) {
                    if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046045261231749") != -1) {
                        if (!StrUtil.isEmpty(zxglXiangMuBianGengEntity.getBghxmmc())) {
                            zxglXiangMuLiXiangMingXiEntity.setXmmc(zxglXiangMuBianGengEntity.getBghxmmc());
                        }
                    }
                    if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046075254699653") != -1) {
                        if (!StrUtil.isEmpty(zxglXiangMuBianGengEntity.getBghzt())) {
                            zxglXiangMuLiXiangMingXiEntity.setZt(zxglXiangMuBianGengEntity.getBghzt());
                            xmzt = zxglXiangMuBianGengEntity.getBghzt();
                        }
                    }
                    if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046159518267013") != -1) {
                        if (zxglXiangMuBianGengEntity.getBghjhksrq() != null) {
                            zxglXiangMuLiXiangMingXiEntity.setJhkssj(zxglXiangMuBianGengEntity.getBghjhksrq());
                        }
                    }
                    if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046191248176773") != -1) {
                        if (zxglXiangMuBianGengEntity.getBghjhjsrq() != null) {
                            zxglXiangMuLiXiangMingXiEntity.setJhjssj(zxglXiangMuBianGengEntity.getBghjhjsrq());
                        }
                    }
                    if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046232260081285") != -1) {
                        if (!StrUtil.isEmpty(zxglXiangMuBianGengEntity.getBghgzmb())) {
                            zxglXiangMuLiXiangMingXiEntity.setGzmb(zxglXiangMuBianGengEntity.getBghgzmb());
                        }
                    }
                    if (zxglXiangMuBianGengEntity.getBglx().indexOf("672051552483047045") != -1) {
                        if (zxglXiangMuBianGengEntity.getBghjhz() != null) {
                            zxglXiangMuLiXiangMingXiEntity.setMbz(zxglXiangMuBianGengEntity.getBghjhz());
                        }
                    }
                }
            }
            boolean update = zxglXiangMuLiXiangMingXiService.updateById(zxglXiangMuLiXiangMingXiEntity);
            if (update && StrUtil.isNotEmpty(xmzt)) {
                QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getXmid, zxglXiangMuLiXiangMingXiEntity.getId());
                List<ZXGL_XiangMuFenJie_mingxibiaoEntity> list = zxglXiangMuFenJieMingxibiaoService.list(queryWrapper);
                if (list != null && list.size() > 0) {
                    for (ZXGL_XiangMuFenJie_mingxibiaoEntity item : list) {
                        item.setXmzt(xmzt);
                    }
                    update = zxglXiangMuFenJieMingxibiaoService.updateBatchById(list);
                }
            }
            return update;
        }
        return true;
    }

    @Override
    public List<ZXGL_XiangMuBianGengEntity> getList(String xmId) {
        QueryWrapper<ZXGL_XiangMuBianGengEntity> zXGL_XiangMuBianGengQueryWrapper = new QueryWrapper<>();
        zXGL_XiangMuBianGengQueryWrapper.lambda().eq(ZXGL_XiangMuBianGengEntity::getXmid, xmId);
        return this.list(zXGL_XiangMuBianGengQueryWrapper);
    }

    @Override
    public ZXGL_XiangMuBianGengEntity getBianGengById(String Id) {
        ZXGL_XiangMuLiXiang_MingXiEntity entity = zxglXiangMuLiXiangMingXiService.getInfo(Id);
        if (null != entity) {
            ZXGL_XiangMuBianGengEntity model = new ZXGL_XiangMuBianGengEntity();
            model.setXmid(entity.getId());
            model.setXmlxdid(entity.getXmlxdid());
            model.setXmmc(entity.getXmmc());
            model.setXmrwlx(entity.getXmlx());
            model.setBgqjhz(entity.getMbz());
            model.setBgqgzmb(entity.getGzmb());
            model.setBgqjhksrq(entity.getJhkssj());
            model.setBgqjhjsrq(entity.getJhjssj());
            model.setBgqzt(entity.getZt());
            model.setBgqxmmc(entity.getXmmc());
            model.setZrr(entity.getZzr());
            return model;
        }
        return null;
    }

    @Override
    public void delete(ZXGL_XiangMuBianGengEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}